package com.integrate.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 刘志强
 * @date 2020/8/18 14:07
 */
@Configuration
public class RabbitmqConfig {

    /**
     * 创建一个队列
     * @return
     */
    @Bean(name="queue")
    public Queue queue() {
        return new Queue("queue");
    }

    @Bean(name="memberQueue")
    public Queue memberQueue() {
        return new Queue("memberQueue");
    }


    /**
     * 创建一个队列作为延迟队列
     * @return
     */
    @Bean(name="delayQueue")
    public Queue delayQueue() {
        Map<String, Object> args = new HashMap<>(2);
        // x-dead-letter-exchange    将队列绑定至交换机
        args.put("x-dead-letter-exchange", "deathExchange");
        // x-dead-letter-routing-key  当消息死亡时,转发给deathExchange交换机的路由
        args.put("x-dead-letter-routing-key", "death-route");
        return new Queue("delayQueue",true, false, false,args);
    }

    /**
     * 创建一个队列作为死信队列
     * @return
     */
    @Bean(name="deathQueue")
    public Queue deathQueue() {
        return new Queue("deathQueue");
    }

    /**
     * 创建一个交换机 用于绑定死信队列
     * @return
     */
    @Bean(name = "deathExchange")
    public TopicExchange deathExchange() {
        return new TopicExchange("deathExchange");
    }

    /**
     * 将死信列绑定至交换机 并设置路由健
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    Binding deathExchangeDeathQueue(@Qualifier("deathQueue") Queue queue, @Qualifier("deathExchange") TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("death-route");
    }

    /**
     * 创建路由交换机 根据路由匹配转发消息给队列
     * @return
     */
    @Bean(name = "exchange")
    public TopicExchange exchange() {
        return new TopicExchange("exchange");
    }


    /**
     * 配置交换机(广播) 转发消息给旗下所有队列
     * @return
     */
    @Bean(name = "fanoutExchange")
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }



    /**
     * 将队列进绑定至路由交换机并设置路由键
     * 交换机会将消息传递给 满足路由键的队列
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    Binding bindingExchangeQueue(@Qualifier("queue") Queue queue, @Qualifier("exchange") TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("exchange.queue");
    }

    @Bean
    Binding bindingExchangeMemberQueue(@Qualifier("memberQueue") Queue memberQueue, @Qualifier("exchange") TopicExchange exchange) {
        // *表示一个词,#表示零个或多个词
        return BindingBuilder.bind(memberQueue).to(exchange).with("exchange.*");
    }

    /**
     * 将队列绑定至广播交换机
     * 因为不绑定路由键 所以交换机会把消息传递给被绑定的所由队列 广播交换机无法设置路由键。因为消息会发给旗下的所有队列
     * @param queue
     * @param fanoutExchange
     * @return
     */
    @Bean
    Binding bindingFanoutExchangeQueueTwo(@Qualifier("queue") Queue queue, @Qualifier("fanoutExchange") FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queue).to(fanoutExchange);
    }
    @Bean
    Binding bindingFanoutExchangeMemberQueueTwo(@Qualifier("memberQueue") Queue memberQueue, @Qualifier("fanoutExchange") FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(memberQueue).to(fanoutExchange);
    }
}
